/**
 * 平台状态管理
 * @author 王有余
 * @date 2022-03-16
 * @version 1.0
 * @desc
 * 管理动态应用栏呈现的状态
 *
 */

import { ref, computed, reactive } from "vue";
import { defineStore } from "pinia";

interface MenuApp {
  名称: string;
  路由: string;
  图标: string;
  缩写: string;
  类型: string;
  show?: boolean;
}

interface FileInfo {
  appId?: string; // 应用缩写
  appName?: string; // 应用名称
  fileId?: string; // 文件缩写
  fileName?: string; // 文件名称
  fileType?: string; // 文件类型
  pathUrl?: string; // 文件路径Url
}

export const useCounterStore = defineStore("counter", () => {
  /**
   * 计数器
   */
  let count = ref<MenuApp[]>([]);
  const doubleCount = computed(() => [
    ...new Set(count.value.map((item) => JSON.stringify(item))),
  ]);
  function increment(nu: MenuApp) {
    count.value.push(nu);
    count.value = doubleCount.value.map((item) => JSON.parse(item));
    // console.log('输出重复后的值：', doubleCount.value.map(item => JSON.parse(item)))
  }
  /**
   * end
   */

  /**
   * isLogin
   * 登录状态
   */
  let isLogin = ref(false);
  const login = computed(() => {
    isLogin.value = true;
  });
  const logout = computed(() => {
    isLogin.value = false;
  });
  /**
   * end
   */
  /**
   * isInitialUser
   * 是否是初始用户
   * 来源：登录后根据登录请求的返回信息，判断是否是初始用户
   */
  const isInitialUser = ref(false)
  const setInitialUser = computed(() => {
    isInitialUser.value = true;
  });
  const setNotInitialUser = computed(() => {
    isInitialUser.value = false;
  });
  /**
   * end
   */
  /**
   * 动态应用栏
   */
  let dynamicApp = ref<MenuApp[]>([]);
  const activeapp = computed(() => [
    ...new Set(dynamicApp.value.map((item) => JSON.stringify(item))),
  ]);
  function pushDynamicApp(routeMenu: MenuApp) {
    if (dynamicApp.value.some((item) => item.缩写 === routeMenu.缩写)) {
      return;
    }
    dynamicApp.value.push(routeMenu);
    console.log("输出应用栏：", dynamicApp.value);
    dynamicApp.value = activeapp.value.map((item) => JSON.parse(item));
    console.log(
      "输出重复后的值：",
      activeapp.value.map((item) => JSON.parse(item))
    );
  }
  function removeDynamicApp(codenum: number) {
    dynamicApp.value.splice(codenum, 1);
  }
  function updateDynamicApp(缩写: string, show: boolean) {
    dynamicApp.value.find((item) => {
      if (item.缩写 === 缩写) {
        console.log("找到该应用", item);
        item.show = show;
      }
    });
  }
  /**
   * end
   */

  /**
   * 用于暂存打开文件信息
   * */
  let openFileInfo = ref<FileInfo>({});
  function setOpenFileInfo(file: any) {
    openFileInfo.value = file;
  }
  /**
   * end
   */

  /**
   * 定义api认证token 值
   */
  const token = ref();
  const info = ref();
  function setToken(t: string) {
    token.value = t;
  }
  function setInfo(t: any) {
    info.value = t;
  }
  return {
    count,
    doubleCount,
    increment,
    dynamicApp,
    pushDynamicApp,
    removeDynamicApp,
    updateDynamicApp,
    login,
    logout,
    isLogin,
    isInitialUser,
    setInitialUser,
    setNotInitialUser,
    token,
    setToken,
    info,
    setInfo,
    openFileInfo,
    setOpenFileInfo,
  };
});
